#!/usr/bin/python3
import re
n = 0
print("1.查找出passwd文件里有多少行里包含sbin字符串")
f = open('passwd')  
for i in f:
    if re.findall("sbin",i):
        n = n + 1
print(n)
f.close()
print("===================")
print("2.查找出passwd文件里uid是3位数字的uid")
f = open('passwd')
for u in f:
    if re.findall("[0-9]{3}",u):
        print(re.findall("[0-9]{3}",u))
f.close()
print("3.查找出用户名里包含feng，uid是4位数字，shell是bash的行")
f = open('passwd')
u_n = re.compile(r'kong',re.I)
u_u = re.compile(r'[0-9]{4}')
u_b = re.compile(r"bash$")
for name in f:
    if u_n.findall(name):
        if u_u.findall(name):
            if u_b.findall(name):
                print(name)
f.close()
print("4.将passwd文件里的sbin替换为cali")
f = open('passwd')
for s in f:
    print(re.sub("sbin","cali",s))

f.close()
print("5.查找出/var/log/secure文件里的ip地址")
f = open('secure')
for ip in f:
    #if re.findall("\d+\.\d+\.\d+\.\d+",ip):
    if re.findall("\d+(\.\d+){3}",ip):
        print(re.findall("\d+\.\d+\.\d+\.\d+",ip))

f.close()
print("6.查找出下面文本里的邮箱地址")

f = open('email.txt')

for e in f:
    if re.findall("\w+@.*\.com",e):
        print(re.findall("\w+@.*\.com",e))

f.close()
'''
cali feng 695811769@qq.com
sina fengdeyong@sina.com
163 1890869feng@163.com
facebook  califeng@facebook.cn
baidu  cali_deyong@baidu.com
nongda  nongda_feng@nongda.org
'''
print("7.查找出下面文本里的网站")
f = open('web.txt')
for w in f:
    m = re.match(r'(\w+.*) ([a-z]+\://\w+\..*) (\w+.*)',w)
    print(m.expand(r'\2'))
#    print(m.expand(r'\2 \1\3'))
'''
baidu http://www.baidu.com  python
sanle  ftp://192.168.0.1   feng
qq   http://www.qq.com/cc.html  43
sina   http://www.sina.com/a.html  hh
github  https://www.gittee.com  zhou
HTTP    https://www.kernel.org/pub/  liu
GIT     https://git.kernel.org/  yang
RSYNC   rsync://rsync.kernel.org/pub/  hu
'''
f.close()
